/*     */ package com.zimbra.cs.service.mail;
/*     */ 
/*     */ import com.zimbra.common.calendar.TimeZoneMap;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.fb.FreeBusy;
/*     */ import com.zimbra.cs.fb.FreeBusy.Interval;
/*     */ import com.zimbra.cs.fb.FreeBusyQuery;
/*     */ import com.zimbra.cs.mailbox.CalendarItem.Instance;
/*     */ import com.zimbra.cs.mailbox.calendar.Util;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Collection;
/*     */ import java.util.HashMap;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class CheckRecurConflicts
/*     */   extends ExpandRecur
/*     */ {
/*     */   public Element handle(Element request, Map<String, Object> context)
/*     */     throws ServiceException
/*     */   {
/*  46 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*  47 */     Account authAcct = getAuthenticatedAccount(zsc);
/*  48 */     Element response = getResponseElement(zsc);
/*     */     
/*  50 */     long rangeStart = request.getAttributeLong("s", 0L);
/*  51 */     if (rangeStart == 0L)
/*  52 */       rangeStart = System.currentTimeMillis();
/*  53 */     long rangeEnd = request.getAttributeLong("e", 0L);
/*  54 */     if (rangeEnd == 0L)
/*  55 */       rangeEnd = Long.MAX_VALUE;
/*  56 */     boolean allInstances = request.getAttributeBool("all", false);
/*  57 */     String exApptUid = request.getAttribute("excludeUid", null);
/*     */     
/*     */ 
/*  60 */     TimeZoneMap tzmap = new TimeZoneMap(Util.getAccountTimeZone(authAcct));
/*  61 */     ExpandRecur.ParsedRecurrence parsed = parseRecur(request, tzmap);
/*  62 */     List<CalendarItem.Instance> instances = getInstances(parsed, rangeStart, rangeEnd);
/*  63 */     if ((instances == null) || (instances.isEmpty())) {
/*  64 */       return response;
/*     */     }
/*     */     
/*  67 */     long rangeStartActual = rangeEnd;long rangeEndActual = rangeStart;
/*  68 */     for (CalendarItem.Instance inst : instances) {
/*  69 */       if ((inst.hasStart()) && (inst.hasEnd())) {
/*  70 */         rangeStartActual = Math.min(rangeStartActual, inst.getStart());
/*  71 */         rangeEndActual = Math.max(rangeEndActual, inst.getEnd());
/*     */       }
/*     */     }
/*  74 */     if (rangeStartActual >= rangeEndActual) {
/*  75 */       return response;
/*     */     }
/*     */     
/*  78 */     FreeBusyQuery fbQuery = new FreeBusyQuery((HttpServletRequest)context.get("servlet.request"), zsc, authAcct, rangeStartActual, rangeEndActual, exApptUid);
/*     */     
/*  80 */     for (Iterator<Element> usrIter = request.elementIterator("usr"); usrIter.hasNext();) {
/*  81 */       Element usrElem = (Element)usrIter.next();
/*  82 */       int folderId = (int)usrElem.getAttributeLong("l", -1L);
/*  83 */       if ((folderId == 1) || (folderId == 0))
/*  84 */         folderId = -1;
/*  85 */       String id = usrElem.getAttribute("id", null);
/*  86 */       if (id != null)
/*  87 */         fbQuery.addAccountId(id, folderId);
/*  88 */       String name = usrElem.getAttribute("name", null);
/*  89 */       if (name != null)
/*  90 */         fbQuery.addEmailAddress(name, folderId);
/*     */     }
/*  92 */     Collection<FreeBusy> fbResults = fbQuery.getResults();
/*  93 */     List<UserConflicts> conflicts = new ArrayList();
/*  94 */     for (FreeBusy fb : fbResults) {
/*  95 */       UserConflicts ucon = getConflicts(fb, instances);
/*  96 */       conflicts.add(ucon);
/*     */     }
/*     */     
/*     */ 
/* 100 */     for (CalendarItem.Instance inst : instances) {
/* 101 */       Element instElem = addInstance(response, inst);
/* 102 */       int numConflicts = 0;
/* 103 */       for (UserConflicts ucon : conflicts) {
/* 104 */         String fbStatus = ucon.get(inst);
/* 105 */         if (fbStatus != null) {
/* 106 */           numConflicts++;
/* 107 */           String username = ucon.getUsername();
/* 108 */           Element usrElem = instElem.addElement("usr");
/* 109 */           usrElem.addAttribute("name", username);
/* 110 */           usrElem.addAttribute("fb", fbStatus);
/*     */         }
/*     */       }
/* 113 */       if ((numConflicts == 0) && (!allInstances))
/* 114 */         instElem.detach();
/*     */     }
/* 116 */     return response;
/*     */   }
/*     */   
/*     */   private static class UserConflicts {
/*     */     private String mUsername;
/*     */     private Map<CalendarItem.Instance, String> mMap;
/*     */     
/*     */     public UserConflicts(String username) {
/* 124 */       this.mUsername = username;
/* 125 */       this.mMap = new HashMap();
/*     */     }
/*     */     
/* 128 */     public String getUsername() { return this.mUsername; }
/*     */     
/*     */     public void put(CalendarItem.Instance inst, String fbStatus) {
/* 131 */       this.mMap.put(inst, fbStatus);
/*     */     }
/*     */     
/*     */     public String get(CalendarItem.Instance inst) {
/* 135 */       return (String)this.mMap.get(inst);
/*     */     }
/*     */   }
/*     */   
/*     */   private static UserConflicts getConflicts(FreeBusy fb, List<CalendarItem.Instance> instances) throws ServiceException {
/* 140 */     UserConflicts conflicts = new UserConflicts(fb.getName());
/* 141 */     Iterator<FreeBusy.Interval> ivalIter = fb.iterator();
/* 142 */     Iterator<CalendarItem.Instance> instIter = instances.iterator();
/* 143 */     FreeBusy.Interval ival = ivalIter.hasNext() ? (FreeBusy.Interval)ivalIter.next() : null;
/* 144 */     CalendarItem.Instance inst = instIter.hasNext() ? (CalendarItem.Instance)instIter.next() : null;
/*     */     
/*     */ 
/* 147 */     String prevStatus = null;
/* 148 */     while ((ival != null) && (inst != null)) {
/* 149 */       String status = ival.getStatus();
/* 150 */       if ("F".equals(status)) {
/* 151 */         ival = ivalIter.hasNext() ? (FreeBusy.Interval)ivalIter.next() : null;
/*     */ 
/*     */       }
/* 154 */       else if ((!inst.hasStart()) || (!inst.hasEnd())) {
/* 155 */         inst = instIter.hasNext() ? (CalendarItem.Instance)instIter.next() : null;
/* 156 */         prevStatus = null;
/*     */       }
/*     */       else {
/* 159 */         long instStart = inst.getStart();
/* 160 */         long instEnd = inst.getEnd();
/* 161 */         long ivalStart = ival.getStart();
/* 162 */         long ivalEnd = ival.getEnd();
/*     */         
/* 164 */         if (instEnd <= ivalStart) {
/* 165 */           inst = instIter.hasNext() ? (CalendarItem.Instance)instIter.next() : null;
/* 166 */           prevStatus = null;
/*     */ 
/*     */ 
/*     */         }
/* 170 */         else if (ivalEnd <= instStart) {
/* 171 */           ival = ivalIter.hasNext() ? (FreeBusy.Interval)ivalIter.next() : null;
/* 172 */           prevStatus = null;
/*     */ 
/*     */         }
/*     */         else
/*     */         {
/* 177 */           if (prevStatus != null)
/* 178 */             status = FreeBusy.chooseBusier(status, prevStatus);
/* 179 */           conflicts.put(inst, status);
/* 180 */           prevStatus = status;
/*     */           
/*     */ 
/* 183 */           if (ivalEnd < instEnd)
/*     */           {
/*     */ 
/* 186 */             ival = ivalIter.hasNext() ? (FreeBusy.Interval)ivalIter.next() : null;
/*     */           }
/*     */           else
/*     */           {
/* 190 */             inst = instIter.hasNext() ? (CalendarItem.Instance)instIter.next() : null;
/* 191 */             prevStatus = null;
/*     */           }
/*     */         } } }
/* 194 */     return conflicts;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/mail/CheckRecurConflicts.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */